package com.example.principle.ocp;

public abstract class AlertHandler implements Priority {
    protected AlertRule rule;
    protected Notification notification;


    public AlertHandler(AlertRule rule,Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    public abstract void adaptiveAlert(ApiInfo apiInfo,AlertContext context);

}

class TpsAlertHandler extends AlertHandler {

    @Override
    public int getPriority() {
        return -1;
    }

    public TpsAlertHandler(AlertRule rule, Notification notification) {
        super(rule, notification);
    }

    @Override
    public void adaptiveAlert(ApiInfo apiInfo,AlertContext context) {
        long tps = apiInfo.getRequstCount() / apiInfo.getDurationOfSeconds();
        if (tps > rule.getMatchedRule(apiInfo.getApi()).getMaxTps()) {
            notification.notify(Notification.NotificationEmergencyLevel.URGENCY,".........");
        }
    }
}

class ErrorCountAlertHandler extends AlertHandler{

    @Override
    public int getPriority() {
        return 1;
    }

    public ErrorCountAlertHandler(AlertRule rule, Notification notification) {
        super(rule, notification);
    }

    @Override
    public void adaptiveAlert(ApiInfo apiInfo,AlertContext context) {
        if (apiInfo.getErrorCount() > rule.getMatchedRule(apiInfo.getApi()).getMaxErrorCount()) {
            notification.notify(Notification.NotificationEmergencyLevel.URGENCY,"........");
        }
    }
}


class TimeoutCountAlertHandler extends AlertHandler {

    @Override
    public int getPriority() {
        return 3;
    }

    public TimeoutCountAlertHandler(AlertRule rule, Notification notification) {
        super(rule, notification);
    }

    @Override
    public void adaptiveAlert(ApiInfo apiInfo,AlertContext context) {
        //
    }
}
